package xin.marcher.module.diplomacy.manager.custom.db;

import com.google.common.collect.Sets;
import org.springframework.stereotype.Service;
import xin.marcher.module.common.enums.DelFlagEnum;
import xin.marcher.module.diplomacy.domain.entity.ProductDetailDO;
import xin.marcher.module.diplomacy.manager.common.cache.RedisStringCache;
import xin.marcher.module.diplomacy.manager.common.db.AbstractRedisStringDatabase;
import xin.marcher.module.diplomacy.repository.ProductRepository;

import javax.annotation.Resource;
import java.util.*;

/**
 * 商品综合信息
 */
@Service("productDetailStringDatabase")
public class ProductDetailStringDatabase extends AbstractRedisStringDatabase<ProductDetailDO> {

    @Resource
    private ProductRepository productRepository;
    /**
     * 表名
     */
    private static final String TABLE_NAME = "sku_info";

    /**
     * sku ID
     */
    public static final String SKU_ID = "skuId";

    /**
     * sku ID
     */
    public static final String SKU_ID_FIELD = "sku_id";


    @Override
    public String getTableName() {
        return TABLE_NAME;
    }

    @Override
    public String getBloomField() {
        return SKU_ID_FIELD;
    }

    @Override
    public Set<String> getTableNameSet() {
        return Sets.newHashSet(getTableName());
    }

    @Override
    public Optional<ProductDetailDO> getTableData(Map<String, Object> tableFieldsMap, String queryType) {
        if (tableFieldsMap.containsKey(SKU_ID)) {
            String skuId = (String) tableFieldsMap.get(SKU_ID);
            ProductDetailDO productDetailDO = productRepository.queryProductDetail(skuId);
            if (!Objects.isNull(productDetailDO) && DelFlagEnum.EFFECTIVE.getCode().equals(productDetailDO.getDelFlag())) {
                return Optional.of(productDetailDO);
            }
            return Optional.empty();
        }
        return Optional.empty();
    }

    @Override
    public Optional<List<ProductDetailDO>> listTableData(Map<String, Object> tableFieldsMap, String queryType) {
        return Optional.empty();
    }

    @Override
    public Optional<RedisStringCache<ProductDetailDO>> listTableDataByIdSet(Set<Long> idSet, String queryType) {
        RedisStringCache redisStringCache = new RedisStringCache();
        List<ProductDetailDO> addList = new ArrayList<>();
        for (Long skuId : idSet) {
            ProductDetailDO productDetailDO = productRepository.queryProductDetail(String.valueOf(skuId));
            if (!Objects.isNull(productDetailDO)) {
                addList.add(productDetailDO);
            }
        }
        redisStringCache.setAddList(addList);
        return Optional.of(redisStringCache);
    }

    @Override
    public Optional<RedisStringCache<ProductDetailDO>> listTableDataByIdSet(List<Long> idSet, String queryType) {
        Long skuId = idSet.get(0);
        ProductDetailDO productDetailDO = productRepository.queryProductDetail(String.valueOf(skuId));
        RedisStringCache redisStringCache = new RedisStringCache();
        redisStringCache.setAddList(Arrays.asList(productDetailDO));
        return Optional.of(redisStringCache);
    }
}
